package com.example.newinfinity.View.ColorPicker

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Paint.Style
import android.graphics.SweepGradient
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import com.example.newinfinity.MyViewModel.DrawViewModel

//仅仅是调色盘视图中的色盘圆圈而非调色盘弹窗
class ColorPickerPalette(context: Context, attrs: AttributeSet? = null) : View(context, attrs) {
    //记录当前小圆圈中的颜色
    var currentSelectedColor:Int = 0

    //调色盘的视图信息
    private var centerX: Int = 0
    private var centerY: Int = 0
    private var mRadius: Int = 0
    private val mPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        this.style = Style.FILL
    }

    //触摸调色盘显示的小圆的视图信息
    private var littleX: Float = 0f
    private var littleY: Float = 0f
    private var littleRadius = 32f
    //HSV中的色相 饱和度 明亮度的参数
    private var littleH = 0f
    private var littleS = 1f
    private var littleV = 1f
    private var littlePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        this.color = Color.WHITE
        this.style = Style.FILL
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        //调色盘的视图信息
        centerX = measuredWidth / 2
        centerY = centerX
        mRadius = measuredWidth / 2
        //调色盘颜色渐变视图设置
        mPaint.shader = SweepGradient(
            centerX.toFloat(),
            centerY.toFloat(),
            intArrayOf(
                0xFFFF0000.toInt(),
                0xFFFFFF00.toInt(),
                0xFF00FF00.toInt(),
                0xFF00FFFF.toInt(),
                0xFF0000FF.toInt(),
                0xFFFF00FF.toInt(),
                0xFFFF0000.toInt()
            ),
            null
        )
        //小圆默认显示在调色盘中心位置
        littleX = centerX.toFloat()
        littleY = centerY.toFloat()
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        when (event?.action) {
            //配置触摸调色盘显示的小圆的视图信息
            MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE -> {
                littleX = event.x
                littleY = event.y
                val radians = Math.atan2((event.y - centerY).toDouble(), (event.x - centerX).toDouble())
                littleH = Math.toDegrees(radians).toFloat()
                if (littleH < 0) {
                    littleH += 360
                }
                littlePaint.color = Color.HSVToColor(floatArrayOf(littleH, littleS, littleV))
                //给小圆设置外围的阴影 更易查看
                littlePaint.setShadowLayer(5f,0f,0f,Color.BLACK)
                invalidate()
            }
            MotionEvent.ACTION_UP -> {
                //选择或修改了调色盘中的颜色后同步修改绘制的图形
                DrawViewModel.getDrawViewModel().mColor = littlePaint.color
                currentSelectedColor = littlePaint.color
                invalidate()
            }
        }
        return true
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        littlePaint.color = Color.HSVToColor(floatArrayOf(littleH, littleS, littleV))
        //选择或修改了调色盘中的颜色后同步修改绘制的图形
        DrawViewModel.getDrawViewModel().mColor = littlePaint.color
        canvas.drawCircle(centerX.toFloat(), centerY.toFloat(), mRadius.toFloat(), mPaint)
        canvas.drawCircle(littleX, littleY, littleRadius, littlePaint)
    }

    //让外界修改明亮度 及饱和度的值
    fun changeSColor(s:Float){
        littleS = s
        invalidate()
    }
    fun changeVColor(v:Float){
        littleV = v
        invalidate()
    }
}